package test3;

import java.util.Random;

public class Solution1 {//老鼠喝药问题
    public static void main(String[] args) {
        Random random = new Random();
        int liquid = random.nextInt(1111);
        int poison = random.nextInt(liquid);

        System.out.println("随机出得到液体瓶数量：" + liquid);
        System.out.println("随机出的毒药编号为：" + poison);
        System.out.println("老鼠测试出毒药编号为：" + getPoison(poison, liquid));
    }

    private static int getPoison(int poison, int liquid) {
        int mouse = 0, i = 0, res = 0, flag;
        flag = liquid;
        int[] id = new int[20];
        while (liquid > 0) {//老鼠喝下全部指定二进制位为1的液体
            if ((poison & 1) == 1) {//喝下编号转化为二进制后当前位为1的毒药老鼠死掉
                id[i++] = 1;
            } else {//喝下编号转化为二进制后当前位为1的毒药老鼠存活
                id[i++] = 0;
            }
            mouse++;
            poison = poison >> 1;
            liquid = liquid >> 1;
        }
        if (flag == 2){//特殊判断只有两瓶的时候
          mouse = 1;
        }
        System.out.println("需要老鼠的数量：" + mouse);

        for (int j = id.length - 2; j >= 0; j--) {
            res = 2 * (id[j] + res);
        }
        return res / 2;//除掉第一次多乘的2
    }
}
